﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using ChiDao.Component.Utils;
using ChiDao.Business.Service.Helper.Logging;
using ChiDao.Business.Service.Impl;
using ChiDao.Site.Models;
using ChiDao.Business.Service;
using System.ComponentModel;
using ChiDao.Business.Models.Account;
using Ext.Net;
using Ext.Net.MVC;

namespace ChiDao.Site.Web.Controllers
{
    [Export]
    public class AccountController : Controller
    {
        #region 属性

        [Import]
        public IAccountService AccountContract { get; set; }

        #endregion

        #region 视图功能
        public ActionResult Index()
        {
            var personInfoes = AccountContract.Persons.Select(m => new PersonView
            {
                ID = m.ID,
                UserName = m.UserName,
                NickName = m.NickName,
                Email = m.Email,
                AddDate = m.AddDate,
                IsDeleted = m.IsDeleted
            });
            return View(personInfoes.ToList());
        }

        [Description("用户登录（无参数）")]
        public ActionResult Login()
        {
            string returnUrl = Request.Params["returnUrl"];
            returnUrl = returnUrl ?? Url.Action("Index", "Main", new { area = "" });
            //returnUrl = returnUrl ?? Url.Action("QueryBaklogTask", "BaklogTask", new { area = "" });
            LoginModel model = new LoginModel
            {
                ReturnUrl = returnUrl
            };
            //Member userInfo = AccountContract.Members.FirstOrDefault(m => m.UserName == model.Account);
            //Session["Member"] = userInfo;

            return View(model);
        }

        [HttpPost]
        [Description("用户登录（带参数）")]
        public ActionResult Login(LoginModel model)
        {
            try
            {
                OperationResult result = AccountContract.Login(model);
                string msg = result.Message ?? result.ResultType.ToDescription();
                if (result.ResultType == OperationResultType.Success)
                {
                    //将用户信息写入Session
                    //Member userInfo = AccountContract.Members.FirstOrDefault(m => m.UserName == model.Account);
                    //Session["Member"] = userInfo;

                    return Redirect(model.ReturnUrl);
                }
                ModelState.AddModelError("", msg);
                return View(model);
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);
                return View(model);
            }
        }

        [Description("用户注销")]
        public ActionResult Logout()
        {
            string returnUrl = Request.Params["returnUrl"];
            //returnUrl = returnUrl ?? Url.Action("QueryBaklogTask", "BaklogTask", new { area = "" });
            if (User.Identity.IsAuthenticated)
            {
                AccountContract.Logout();
            }
            //如果返回地址为空，则跳转到登录界面
            if (String.IsNullOrEmpty(returnUrl))
            {
                returnUrl = "/Account/Login";
            }
            return Redirect(returnUrl);
        }

        public ActionResult AddClick()
        {
            return new Ext.Net.MVC.PartialViewResult { ViewName = "Add" };   //注意此处应该是Ext.Net.MVC.PartialViewResult
        }

        public ActionResult Add(PersonView view)
        {
            OperationResult result = AccountContract.Add(view);
            if (result.ResultType == OperationResultType.Success)
            {
                X.Msg.Confirm("Confirm", "新增成功", new MessageBoxButtonsConfig
                {
                    Ok = new MessageBoxButtonConfig { Text = "Ok" }
                }).Show();
                return this.Direct();
            }
            else
            {
                X.Msg.Confirm("Confirm", "新增失败", new MessageBoxButtonsConfig
                {
                    Ok = new MessageBoxButtonConfig { Text = "Ok" }
                }).Show();
                return this.Direct();
            }
        }
        #endregion
    }
}